//Refactored
package com.example.sleepdebtcalculatorandroid;

import java.util.Date;
import com.example.sleepdebtcalculatorandroid.R.id;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class ActualSleepAmountActivity extends Activity implements CalendarView.OnDateChangeListener, View.OnClickListener {

	private final Context context = this;
	private DatabaseHelper dbHelper;
	private CalendarView calendarView;
	private Dialog dialog;
	private TimePicker timePicker;
	private TextView textView;
	private Date date;
	private SQLiteDatabase db;
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_actual_sleep_amount);
		dbHelper = new DatabaseHelper(this);
		calendarView = (CalendarView)findViewById(id.calendarView1);
		calendarView.setShowWeekNumber(false);
		calendarView.setOnDateChangeListener(this);
		Button editActualAmountSlept = (Button)findViewById(id.editActualAmountSleptButton);
		editActualAmountSlept.setOnClickListener(this);
		initializeAttributes();
	}
	
	private void initializeAttributes() {
		dialog = new Dialog(context);
		dialog.setContentView(R.layout.dialog_actual_sleep_amount_input);
		dialog.setTitle("Enter Actual Amount Slept");
		timePicker = (TimePicker)dialog.findViewById(R.id.editActualAmountSleptTimePicker);
		timePicker.setIs24HourView(true);
		textView = (TextView)dialog.findViewById(R.id.editActualAmountSleptTextView);
		db = dbHelper.getWritableDatabase();
	}
	
	@Override
	public void onClick(View v) {
		date = new Date(calendarView.getDate());
		Cursor cursor = getData(db);
		textView.setText("On " + (date.getMonth()+1)+"/"+date.getDate()+"/2012" + " You Slept For\n   " + displayData(cursor));
		Button saveActualActualSleptButton = (Button)dialog.findViewById(id.saveActualAmountSleptButton);
		saveActualActualSleptButton.setOnClickListener(new OnClickListener() {	
			@Override
			public void onClick(View v) {
				saveData(db, date,timePicker);
				Cursor cursor = getData(db);
				textView.setText("On " + (date.getMonth()+1)+"/"+date.getDate()+"/2012" + " You Slept For\n   " + displayData(cursor));
			}
		});
		dialog.show();	
	}
		
	@Override
	public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
		Cursor cursor = getData(db);
		Toast.makeText(getApplicationContext(), displayData(cursor), 4000).show();
	}
	
	public Cursor getData(SQLiteDatabase db) {
		Date date = new Date(calendarView.getDate());
		String[] selectionArgs = {Integer.toString(date.getDate()), Integer.toString(date.getMonth())};
		Cursor cursor = db.rawQuery("SELECT * from actualSleepAmount where day=? and month=?",selectionArgs);
		return cursor;
	}
	
	public String displayData(Cursor cursor){
		String builder = new String();
		while(cursor.moveToNext()){
			String actualSleepAmountHours = cursor.getString((cursor.getColumnIndex("actualSleepAmountHours")));
			String actualSleepAmountMinutes = cursor.getString((cursor.getColumnIndex("actualSleepAmountMinutes")));
			String actualSleepAmount = actualSleepAmountHours + " Hours and " + actualSleepAmountMinutes + " Minutes";
			builder = actualSleepAmount;
			}
		return builder;
	}
	
	public void saveData(SQLiteDatabase db, Date date, TimePicker timePicker) {
		String[] whereArgs={Integer.toString(date.getDate()), Integer.toString(date.getMonth()) };
		ContentValues contentValues = new ContentValues();
		contentValues.put("actualSleepAmountHours", Integer.toString(timePicker.getCurrentHour()));
		contentValues.put("actualSleepAmountMinutes", Integer.toString(timePicker.getCurrentMinute()));
		db.update("actualSleepAmount", contentValues, "day=? and month=?",whereArgs);
	}
}
